package com.bqy.myokgo;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bqy.myokgo.base.MyBaseActivity;
import com.bqy.myokgo.base.okgo.OKHttp;
import com.bqy.myokgo.base.okgo.bean.XXResponse;
import com.bqy.myokgo.base.okgo.callback.isDialogCallback;
import com.bqy.myokgo.utils.NoDoubleClickListener;
import com.bqy.myokgo.utils.RandomUntil;
import com.bqy.myokgo.utils.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 熊猫先生
 * 2019/1/9:14:30
 */

public class ReActivity extends MyBaseActivity {

    private RecyclerView re;
    private SmartRefreshLayout re_rsh;

    private List<ReBean> beanList = new ArrayList<ReBean>();
    private ReAdapter adapter;
    private LinearLayoutManager manager;

    private int index = 1;
    private boolean isDialog = true;
    private boolean isOK;//是否成功接到了数据

    @Override
    public View setContentView() {
        return View.inflate(this, R.layout.activity_re, null);
    }

    @Override
    public void iniGreate(Bundle savedInstanceState) {
        super.iniGreate(savedInstanceState);
        mToolBar.setToolbarStr("Adapter");
        mToolBar.setToolbarRightView(View.inflate(this, R.layout.view_toolber_right, null));
        mToolBar.view_toolbar_right.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                ToastUtils.show("啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊");
            }
        });

        adapter = new ReAdapter(beanList);
        manager = new LinearLayoutManager(this);
    }

    @Override
    public void iniView() {
        re_rsh = findViewById(R.id.re_rsh);
        re_rsh.setEnableRefresh(true);//开启下拉刷新
        re = findViewById(R.id.re);
        re.setAdapter(adapter);
        re.setLayoutManager(manager);
        adapter.setUpFetchEnable(true);
    }

    @Override
    public void iniClick() {
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.view1:
                        ToastUtils.show("111111111111111111111");
                        break;
                    case R.id.view2:
                        ToastUtils.show("2222222222222222222222");
                        break;
                }
            }
        });
        re_rsh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                re_rsh.autoRefresh();
                isDialog = false;
                index = 1;
                postHttp();
            }
        });
        re_rsh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                re_rsh.autoLoadMore();
                isDialog = false;
                index = isOK ? index + 1 : index;
                postHttp();
            }
        });
    }

    @Override
    public void iniData() {
        postHttp();
    }

    /**
     * adapter
     */
    class ReAdapter extends BaseQuickAdapter<ReBean, BaseViewHolder> {

        public ReAdapter(List<ReBean> data) {
            super(data);
            //Step.1
            setMultiTypeDelegate(new MultiTypeDelegate<ReBean>() {
                @Override
                protected int getItemType(ReBean entity) {
                    //根据你的实体类来判断布局类型
                    return entity.itemType;
                }
            });
            //Step.2
            getMultiTypeDelegate()
                    .registerItemType(ReBean.VIEW1, R.layout.re_view_1)
                    .registerItemType(ReBean.VIEW2, R.layout.re_view_2);
        }

        @Override
        protected void convert(BaseViewHolder helper, ReBean item) {
            switch (helper.getItemViewType()) {
                case ReBean.VIEW1:
                    helper.addOnClickListener(R.id.view1);
                    break;
                case ReBean.VIEW2:
                    helper.addOnClickListener(R.id.view2);
                    break;
            }

        }
    }


    private void postHttp() {
        Map map = new HashMap();
        map.put("UserId", "1709291753573829");
        map.put("Token", "517013db993b2d89ad34a5b5387db7b51.4.9T7D4PZ435BCMDN5GWMD95773B6R73C783CF9B35D919E80DAE33099F4F6E6740D");
        map.put("Pageindex", index);
        map.put("Pagecount", "10");
        map.put("CheckInDate", "2019-01-10");
        map.put("CheckOutDate", "2019-01-11");
        map.put("AreaID", "");
        map.put("CityId", "2511");
        map.put("CurrentItude", "");
        map.put("OneMoney", "0");
        map.put("TwoMoney", "999999");
        map.put("Keyword", "");
        map.put("listSearchCriteria", "[{\"AreaId\":\"0\",\"CityName\":\"成都\",\"Id\":1,\"Latitude\":0.0,\"Longitude\":0.0,\"Name\":\"推荐排序\",\"ParentId\":\"0\",\"Title\":\"Recommend\",\"Type\":16,\"TypeName\":\"排序\"}]");
        OKHttp.post(map, "http://118.190.0.224:10006/api/HotleInfo/HotleInfoSearchNew"
                , new isDialogCallback<XXResponse<AllHotelBean>>(this, isDialog) {
                    @Override
                    public void onSuccess(Response<XXResponse<AllHotelBean>> response) {
                        AllHotelBean bean = response.body().Result;
                        if (index == 1) {
                            beanList.clear();
                        }
                        for (int i = 0; i < bean.hotelList.size(); i++) {
                            if (RandomUntil.getNum(1, 3) == 1) {
                                beanList.add(new ReBean(ReBean.VIEW1));
                            } else {
                                beanList.add(new ReBean(ReBean.VIEW2));
                            }
                        }
                        adapter.notifyDataSetChanged();
                        isOK = true;
                    }

                    @Override
                    public void onError(Response<XXResponse<AllHotelBean>> response) {
                        super.onError(response);
                        isOK = false;
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        re_rsh.finishRefresh();
                        re_rsh.finishLoadMore();
                        isDialog = true;
                    }
                });
    }

    /**
     * bean
     */
    class ReBean implements Serializable {
        public static final int VIEW1 = 1;
        public static final int VIEW2 = 2;

        public int itemType;

        public ReBean(int itemType) {
            this.itemType = itemType;
        }

    }

    class AllHotelBean implements Serializable {
        public int Allcount;
        public int HotelCount;
        public List<HotelListBean> hotelList;
        public int Sort;
    }

    class HotelListBean implements Serializable {
        public boolean IsIllegal;
        public String Message;
        public String HotelInfoID;
        public String SourceHotelCode;
        public String HotelName;
        public String HotelNameEN;
        public String CityName;
        public String CityCode;
        public String RoadCross;
        public boolean isMapClick;
        public boolean isMarker;
    }
}